import {
    goods as goods1
} from '../data/goods.js'
export default class Newinit {
    parent1;
    parent2;
    parent3;
    lis;
    username;
    ems;
    constructor() {
        this.parent1 = document.querySelector('.recommend1')
        this.parent2 = document.querySelector('.recommend2')
        this.parent3 = document.querySelector('.recommend3')
        this.username=document.querySelector('.username')
        this.lis=document.querySelectorAll('.loginuser')
        new recommend(list, this.parent1, 'good1');
        new recommend(list1, this.parent2, 'good2');
        new recommend(goods1, this.parent3, 'good3')
        window.addEventListener('load', e => this.clickhandler(e))
    }
    async clickhandler(e) {
        this.cartext()
        goods1.forEach((t, i) => {
            var s = JSON.stringify(t.skuList)
            t.skuList = s;
        })
        console.log(goods1);
        for (var i = 0; i < goods1.length; i++) {
           
             if (!data) return
            var data = await fetch('http://localhost:4020/adddata', {
                method: 'post',
                body: JSON.stringify(goods1[i])
            });
            data = await data.json();
            console.log(data);
        }
        
    }
    cartext(){
        var s=localStorage.getItem('user');
        if(s){
           this.lis.forEach((item)=>{
               item.style.display='none'
           })
           this.username.style.display='block'
           this.rander(this.username,s)
           this.ems=document.querySelector('em')
           this.ems.addEventListener('click',e=>this.emsclick(e))
        }
        
    }
    emsclick(e){
        localStorage.clear();
        this.username.style.display='none';
        this.lis.forEach((item)=>{
            item.style.display='block'})
    }
    rander(name,s){
        name.innerHTML=`
           亲爱的 <span> ${s} </span> 你好
           <em>退出</em>
        `
    }
}

class recommend {
    elem;
    parent;
    imgs;
    constructor(list, parent, classN) {
        this.list = list
        this.parent = parent;
        this.elem = document.createElement('div');
        this.elem.className = classN;
        this.parent.appendChild(this.elem);
        window.addEventListener('load', e => this.windowload(e))

    }

    windowload(e) {
        if (this.elem.className === 'good3') {
            this.randerGoods(this.elem)
        } else {
            this.list.forEach(item => {
                this.imgs = new Image();
                this.imgs.src = item.img;
                this.elem.appendChild(this.imgs)
            })
        }

    }
    randerGoods(classN) {
        classN.innerHTML = this.list.reduce((v, t) => {
            return v + `
           <div class="goodsdiv" id="${t.productSid}">
            <img src="${t.proPictDir}">
            <p>${t.brandName}</p>
            <span>${t.productName}</span>
            <i>￥${t.minSellingPrice}</i>
            </div>
        `
        }, "")
        classN.addEventListener('click', e => this.localHandler(e))
        this.parent.appendChild(classN)
    }
    async localHandler(e) {

        location.href = './shoppingcar.html' + "?" + e.path[1].id;
    }
}

new Newinit()